//
//  MeasuringRulerView.swift
//  Kosspot
//
//  Created by 胡辉 on 2025/7/10.
//

import SwiftUI

// 尺子类型枚举
enum RulerType {
    case height // 身高，单位cm
    case weight // 体重，单位kg
    case steps  // 步数，单位步
    case weightG
    case heightM
    
    var minValue: Double {
        switch self {
        case .height: return 90
        case .weight: return 30
        case .steps: return 0
        case .weightG: return 30000
        case .heightM: return 0.9
        }
    }
    
    var maxValue: Double {
        switch self {
        case .height: return 230
        case .weight: return 150
        case .steps: return 20000
        case .weightG: return 150000
        case .heightM: return 2.3
        }
    }
    
    var unit: String {
        switch self {
        case .height: return "cm"
        case .weight: return "kg"
        case .steps: return "步"
        case .weightG: return "k"
        case .heightM: return "m"
        }
    }
    var stepInterval: Double {
        switch self {
        case .height: return 1.0
        case .weight: return 1.0
        case .steps: return 100
        case .weightG: return 100
        case .heightM: return 0.01
        }
    }
}

struct MeasuringRulerView: View {
    // 尺子类型
    let type: RulerType
    // 当前选中的值
    @Binding var selectedValue: Double
    // 最小值和最大值
    let minValue: Double
    let maxValue: Double
    // 单位显示
    let unit: String
    
    @State private var offset: CGPoint = CGPoint(x: 0, y: 0)
    
    // 初始化器
    init(type: RulerType, selectedValue: Binding<Double>, minValue: Double, maxValue: Double, unit: String) {
        self.type = type
        self._selectedValue = selectedValue
        self.minValue = minValue
        self.maxValue = maxValue
        self.unit = unit
    }
    
    // 便捷初始化器，提供常用配置
    init(type: RulerType, selectedValue: Binding<Double>) {
        self.type = type
        self._selectedValue = selectedValue
        self.minValue = type.minValue
        self.maxValue = type.maxValue
        self.unit = type.unit
    }
    
    
    // 计算总刻度数
    private var totalMarks: Int {
        Int((maxValue - minValue) / type.stepInterval) + 1
    }
    
    // 计算每个刻度的宽度
    private let markWidth: CGFloat = 10
    
    var body: some View {
        VStack(spacing: 20) {
            ZStack(alignment: .top) {
                GeometryReader { geomtry in
                    ScrollViewReader { proxy in
                        OffsetObservingScrollView(axes: [.horizontal], showsIndicators: false, offset: $offset) {
                            HStack(spacing: 0) {
                                ForEach(0..<totalMarks, id: \.self) { index in
                                    let value = minValue + Double(index) * type.stepInterval
                                    RulerMarkView(value: value, type: type)
                                        .frame(width: markWidth)
                                }
                            }
                            .padding(.horizontal, geomtry.size.width / 2 - markWidth / 2)
                        }
                        .onAppear {
                            let index = Int((selectedValue * 100 - minValue * 100) / (type.stepInterval * 100))
                            proxy.scrollTo(index, anchor: .center)
                        }
                        .onChange(of: type) { newValue in
                            let index = Int((selectedValue * 100 - newValue.minValue * 100) / (newValue.stepInterval * 100))
                            proxy.scrollTo(index, anchor: .center)
                        }
                        .onChange(of: offset) {newValue in
                            selectedValue = (offset.x / markWidth) * type.stepInterval + minValue
                        }

                    }
                    
                }
                .frame(height: 45)
                
                VStack(spacing: 1) {
                    Rectangle()
                        .fill(mainColor)
                        .frame(width: 2, height: 32)
                    Image("triangle")
                        .frame(width: 8, height: 4)
                }
            }
        }
    }
    
    // 最大刻度高度
    private let maxMarkHeight: CGFloat = 40
    
    // 格式化显示的值
    private var valueString: String {
        switch type {
        case .height, .weight:
            return String(format: "%.1f", selectedValue)
        case .steps, .weightG:
            return String(format: "%.0f", selectedValue)
        case .heightM:
            return String(format: "%.2f", selectedValue)
        }
    }
    
    // 滚动偏移量
    @State private var scrollOffset: CGFloat = 0
}

// 单个刻度视图
struct RulerMarkView: View {
    let value: Double
    let type: RulerType
    
    var body: some View {
        VStack {
            // 刻度线
            Rectangle()
                .fill(Color(hex: "#8A8A8A"))
                .frame(width: 1, height: markHeight)
            
            // 数值标签
            if shouldShowLabel {
                Text(labelText)
                    .font(.system(size: 16, weight: .regular))
                    .foregroundColor(.white)
                    .frame(width: 60)
            }
        }
        .frame(maxHeight: .infinity, alignment: .top)
    }
    
    // 刻度高度
    private var markHeight: CGFloat {
        switch type {
        case .height, .weight:
            let remainder = value.truncatingRemainder(dividingBy: 10)
            if remainder == 0 {
                return 32
            } else if remainder == 5 {
                return 24
            } else {
                return 16
            }
        case .steps, .weightG:
            let remainder = value.truncatingRemainder(dividingBy: 1000)
            if remainder == 0 {
                return 32
            } else if remainder == 500 {
                return 24
            } else {
                return 16
            }
        case .heightM:
            let intValue = Int((value * 100).rounded())
            if intValue % 10 == 0 {
                return 32
            } else if intValue % 10 == 5 {
                return 24
            } else {
                return 16
            }
        }
    }
    
    // 是否显示标签
    private var shouldShowLabel: Bool {
        switch type {
        case .height, .weight:
            let roundedValue = round(value)
            let isMultipleOf10 = roundedValue.truncatingRemainder(dividingBy: 10) == 0
            guard isMultipleOf10 else { return false }
            
            let quotient = roundedValue / 10
            return quotient.truncatingRemainder(dividingBy: 2) == 1
        case .steps, .weightG:
            let roundedValue = round(value)
            let isMultipleOf2000 = roundedValue.truncatingRemainder(dividingBy: 2000) == 0
            return isMultipleOf2000
        case .heightM:
            let intValue = Int((value * 100).rounded())
            return intValue % 10 == 0
            
        }
    }
    
    // 标签文本
    private var labelText: String {
        switch type {
        case .height, .weight:
            return String(format: "%.0f", value)
        case .steps, .weightG:
            return String(format: "%.0f", value)
        case .heightM:
            return String(format: "%.1f", value)
        }
    }
}



struct RView: View {
    @State private var defaultHeight: Double = 170
    @State private var defaultSteps: Double = 5000
    @State private var defaultWeight: Double = 80
    @State private var defaultHeightM: Double = 1.7
    var body: some View {
        VStack {
//            MeasuringRulerView(type: .height, selectedValue: $defaultHeight)
//                .padding()
//                .background(Color(.systemBackground))
//            
//            MeasuringRulerView(type: .steps, selectedValue: $defaultSteps)
//                .padding()
//                .background(Color(.systemBackground))
//            MeasuringRulerView(type: .weight, selectedValue: $defaultWeight)
//                .padding()
//                .background(Color(.systemBackground))
            MeasuringRulerView(type: .heightM, selectedValue: $defaultHeightM)
                .padding()
                .background(Color(.systemBackground))
        }
    }
}

#Preview {
    RView()
}

